package com.hotel.servlet;

import com.hotel.bean.Account;
import com.hotel.bean.Hotel;
import com.hotel.bean.Order;
import com.hotel.bean.Room;
import com.hotel.dao.HotelDao;
import com.hotel.dao.OrderDao;
import com.hotel.dao.RoomDao;
import com.hotel.util.DBOperation;
import com.sun.org.apache.xpath.internal.operations.Or;
import org.apache.ibatis.session.SqlSession;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Calendar;
import java.util.Date;

/**
 * @author KungSu.
 * @create 2021-05-11 14:23
 */
@WebServlet(value = "/OrderServlet")
public class OrderServlet extends HttpServlet {
    @Override
    public void init() throws ServletException {
        super.init();
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        Account account = (Account) request.getSession().getAttribute("curUser");
//        SqlSession sqlSession = DBOperation.getSqlSession();
//        OrderDao orderDao = sqlSession.getMapper(OrderDao.class);
//        System.out.println(account.getAccount_id());
//        List<OrderWithHotelName> orders = orderDao.findOrderAndHotelName(account.getAccount_id());
////        DateFormat dFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
////        SimpleDateFormat sFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//
//        for (Order order: orders){
//            System.out.println(order);
//        }
//
//        sqlSession.close();
//        request.setAttribute("order",orders);
//        request.setAttribute("account",account);
//        request.getRequestDispatcher("/jsp/wodedingdanxinxi.jsp").forward(request,response);

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        SqlSession session = DBOperation.getSqlSession();
        HotelDao hotelDao = session.getMapper(HotelDao.class);
        RoomDao roomDao = session.getMapper(RoomDao.class);

        /*预订酒店*/
        int hotelId = Integer.valueOf(request.getParameter("HotelId"));
        //预计住多久
        int days = Integer.valueOf(request.getParameter("HotelDays"));
        String roomType = request.getParameter("HotelType");
//        String url = "OrderHotelServlet?id=" + hotelId;

        /*判断房型*/
        //获取当前酒店信息
        Hotel hotel = hotelDao.findHotleById(Integer.valueOf(hotelId));
        //创建要查找的房间参数
        Room findRoom = new Room();
        findRoom.setHotel_id(hotel.getHotel_id()); //确定酒店
        if (roomType.equals("standard")) {
            if (hotel.getStandard_num() > 0) {
                findRoom.setRoom_type(1);
            } else {
                System.out.println("standard is zero");
                response.getWriter().append("false");
                session.close();
                return;
            }
        } else if (roomType.equals("queen")) {
            if (hotel.getQueen_num() > 0) {
                findRoom.setRoom_type(2);
            } else {
                response.getWriter().append("false");
                session.close();
                return;
            }
        } else if (roomType.equals("room")) {
            if (hotel.getHome_num() > 0) {
                findRoom.setRoom_type(3);
            } else {
                response.getWriter().append("false");
                session.close();
                return;
            }
        } //确定房型

        /*查询房间*/
        Room freeRoom = roomDao.findFreeRoom(findRoom).get(0);
        /*酒店信息中剩余房间数-1， 更新该房间状态*/
        roomDao.updateRoomInfo(freeRoom);
        /*新增一条用户记录*/
        OrderDao orderDao = session.getMapper(OrderDao.class);
        Order order = new Order();
        order.setAccount_id(((Account) request.getSession().getAttribute("curUser")).getAccount_id());
        order.setHotel_id(freeRoom.getHotel_id());
        order.setCheck_in_time(new Date());
        //计算离开时间
        Date nowDate = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(nowDate);
        calendar.add(Calendar.DAY_OF_MONTH, days);
        order.setDeparture_time(calendar.getTime());
        order.setOrder_price(hotelDao.getRoomPrice(freeRoom) * days);
        order.setRoom_number(freeRoom.getRoom_id());
        orderDao.saveOrder(order);
    }
}
